package org.vectrics.config;

import javax.servlet.ServletContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.vectrics.SystemException;
import org.vectrics.hibernate.ApplicationContext;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.user.UserPasswordHelper;

public abstract class ApplicationStarter {
    private Logger log = Logger.getLogger(this.getClass());
    
	abstract public ApplicationContext initializeWebApplication(ServletContext servletContect);
    abstract public boolean isRunning();
    
    /** 
     * Encrypt all unencrypted passwords in user database table.
     */
    protected void encryptAllPasswords() {
    	log.info("Encrypting all passwords");
    	if (ThreadProperties.isTransactionInProgress() == false) { 
    		ThreadProperties.beginTransaction();
    	} 
    	UserPasswordHelper helper = new UserPasswordHelper();
    	helper.encryptAllUserPasswords();
    	ThreadProperties.forcedCommit();
    }

    protected BeanFactory initializeSpring() {
        String fileName = "test/spring-beans.xml";
        return(this.initializeSpring(fileName));
    }

    protected BeanFactory initializeSpring(String springConfigFileName) {
    	BeanFactory beanFactory = null;
        try {
	        beanFactory = new XmlBeanFactory(new FileSystemResource(springConfigFileName));
        } catch (Exception fne) {
            throw new SystemException("Exception initializing beanFactory", fne);
        }
        return(beanFactory);
    }

    protected BeanFactory initializeSpringFromServletFile(ServletContext servletContext) {
        //InputStream input = null;
//        String fileName = "/WEB-INF/spring-beans.xml";
        //input = servletContext.getResourceAsStream(fileName);
        //if (input == null)
        //    throw new SystemException("Could not find servlet resource (Spring bean file): " + fileName);
        try {
//        	Resource resource = new ServletContextResource(servletContext, fileName);
//	        BeanFactory beanFactory = new XmlBeanFactory(resource);
	        BeanFactory beanFactory = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
	        return(beanFactory);
        } catch (Exception ex) {
        	throw new SystemException("Exception initializing Spring", ex);
        }
    }

/*    
    private BeanFactory initializeSpringFromServletFile_VectricsSiteVersion_deprecate(ServletContext servletContext) {
    	BeanFactory beanFactory = null;
    	
        InputStream input = null;
        String fileName = "WEB-INF/spring-beans.xml";
        input = servletContext.getResourceAsStream(fileName);
        
        if (input == null)
            throw new SystemException("Could not find servlet resource (Spring bean file): " + fileName);
            
        beanFactory = new XmlBeanFactory(input);
        
        if (beanFactory.containsBean("hibernate.util") == false) {
        	throw new SystemException("Could not find hibernate.util bean");
        }
        if (beanFactory.containsBean("date.format") == false) {
        	throw new SystemException("Could not find date.format bean");
        }
        if (beanFactory.containsBean("configuration") == false) {
        	throw new SystemException("Could not find configuration bean");
        }
        
        
        if (CoreServiceLocator.getConfiguration() == null)
        	throw new SystemException("Could not find configuration service");
        return(beanFactory);
    }
        	*/
    
    
}
